package org.yray.sip.creator;


import org.yray.sip.base.MsgEle;
import org.yray.sip.server.SipSession;

public class UnauthorizedCreator extends MessageCreator {

    /**
     * SIP/2.0 401 Unauthorized
     * Via: SIP/2.0/UDP 192.168.44.84:56823;rport=56823;branch=z9hG4bKPjb5b0293bdfcd411fa3d3eae27d9df7cb
     * From: "19_60003" <sip:60003@172.168.198.19>;tag=e90d2542148844bcb826f7be66851ff1
     * To: "19_60003" <sip:60003@172.168.198.19>;tag=2DrQm41FN0g9g
     * Call-ID: 16ff29cfa202461e929a4d08cdb437f1
     * CSeq: 35527 REGISTER
     * User-Agent: OE+ PBX
     * Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY, PUBLISH, SUBSCRIBE
     * Supported: path, replaces
     * WWW-Authenticate: Digest realm="172.168.198.19", nonce="7f808da4-225c-4dd3-9d1d-de0b6bf103a0", algorithm=MD5, qop="auth"
     * Content-Length: 0
     * @param session
     * @return
     */
    @Override
    public String createMsg(SipSession session){
        StringBuilder sb = new StringBuilder();
        sb.append("SIP/2.0 401 Unauthorized").append(MsgEle.LINE_SEPARATOR);
        sb.append("Via: SIP/2.0/UDP "+ session.getLocalIpPort()+";rport;branch="+session.getBranch()).append(MsgEle.LINE_SEPARATOR);
        sb.append("From: "+session.getSipExtIpName()+";tag="+session.getTag()).append(MsgEle.LINE_SEPARATOR);
        sb.append("To: "+session.getSipExtIpName()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Call-ID: "+session.getCallId()).append(MsgEle.LINE_SEPARATOR);
        sb.append("CSeq: "+session.getCseq()+" REGISTER").append(MsgEle.LINE_SEPARATOR);
        sb.append("User-Agent: ROBOT NO1").append(MsgEle.LINE_SEPARATOR);
        sb.append("Contact: "+session.getContact()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY, PUBLISH, SUBSCRIBE").append(MsgEle.LINE_SEPARATOR);
        sb.append("Supported: path, replaces").append(MsgEle.LINE_SEPARATOR);
        sb.append("WWW-Authenticate: Digest realm=\""+session.getRealm()+"\", nonce=\""+session.getNonce()+"\", algorithm=MD5, qop=\"auth\"").append(MsgEle.LINE_SEPARATOR);
        sb.append("Content-Length:  0").append(MsgEle.LINE_SEPARATOR);
        sb.append(MsgEle.LINE_SEPARATOR);
        return sb.toString();
    }


}
